/*----------------------------------------------------------------------------------*/
/*!
 * @file tiles.c
 * @brief Implementierung der I/O-Routinen fuer Tiles
 * @author Tisi
 *
 */
/*------------------------------------------------------------------------------------

REVISION CONTROL:
$Id: tiles.c,v 1.1 2006/08/08 20:45:41 tisi Exp $
$ProjectName: $
$ProjectRevision: $

UPDATE:
$Log: tiles.c,v $
Revision 1.1  2006/08/08 20:45:41  tisi
*** empty log message ***


------------------------------------------------------------------------------------*/

#include <string.h>
#include <zlib.h>
#include "tiles.hxx"

typedef struct 
{
  char mark[8];
  unsigned short version;
  unsigned short quantity;
  char reserved[116];
} TileHeader;

/*----------------------------------------------------------------------------------*/
/*!
 * @fn TileIO::TileIO()
 *
 * @brief Konstruktor der Klasse TileIO
 *
 */
/*----------------------------------------------------------------------------------*/
TileIO::TileIO()
{
  tile = NULL;
}

/*----------------------------------------------------------------------------------*/
/*!
 * @fn TileIO::~TileIO()
 *
 * @brief Dekonstruktor der Klasse TileIO
 *
 * Ist Speicher fuer Tiles allokiert worden, so wird er hier freigegeben.
 */
/*----------------------------------------------------------------------------------*/
TileIO::~TileIO()
{
  if (tile!=NULL)
  {
    delete[] tile;
  }
}

/*----------------------------------------------------------------------------------*/
/*!
 * @fn char TileIO::loadTilesFromFile(char *filename)
 *
 * @param filename Dateiname der Tile-Datei
 * 
 * @return Status
 *
 * @brief Laedt eine Tile-Datei
 *
 * Laedt die angegeben Tile-Datei. Tritt ein Fehler auf, so wird ein entsprechender
 * Fehlercode zurueckgegeben.
 */
/*----------------------------------------------------------------------------------*/
char TileIO::loadTilesFromFile(char *filename)
{
  gzFile file;
  TileHeader header;
  char return_value = TLE_OK;
  file = gzopen(filename,"rb");
  if(file != NULL)
  {
    if(gzread(file,&header,sizeof(TileHeader))==sizeof(TileHeader))
    {
      if(header.version==TLE_FILE_VERSION_1)
      {
        tile = new Tile[header.quantity];
        if(gzread(file,(char*)tile,header.quantity*sizeof(Tile))!=(int)sizeof(Tile)*header.quantity)
        {
          return_value = TLE_READ_ERROR;
        }
      }
      else
      {
        return_value = TLE_VERSION_ERROR;
      }
    }
    else
    {
      return_value = TLE_READ_ERROR;
    }
  }
  else
  { 
    return_value = TLE_OPEN_ERROR;
  }
  gzclose(file);
  return return_value;  
}


/*----------------------------------------------------------------------------------*/
/*!
 * @fn Tile* TileIO::getTile(unsigned short id)
 *
 * @param id Tile-Id
 * 
 * @return Pointer zu den Tile_Daten
 *
 * @brief Gibt das Tile mit der Id id zurueck.
 *
 */
/*----------------------------------------------------------------------------------*/
Tile* TileIO::getTile(unsigned short id)
{
  if(id < quantity)
  {
    return &tile[id];
  }
  else
  {
    return NULL;
  }
}

/*----------------------------------------------------------------------------------*/
/*!
 * @fn char TileIO::setTile(unsigned short id, Tile* new_tile)
 *
 * @param id Tile-Id
 * @param new_tile Pointer zu den Tile-Daten
 * 
 * @return Status
 *
 * @brief Setzt das Tile mit der Id id.
 *
 */
/*----------------------------------------------------------------------------------*/

char TileIO::setTile(unsigned short id, Tile* new_tile)
{
  if(id < quantity)
  {
    memcpy((char*)&tile[id],(char*)new_tile,sizeof(Tile));
    return TLE_OK;
  }
  return TLE_ID_ERROR;
}

/*----------------------------------------------------------------------------------*/
/*!
 * @fn void TileIO::createNewTileArray(unsigned short array_size))
 *
 * @param array_size Arraygroesse des neuen Tile-Array
 *
 * @brief Erzeugt eine neues TileArray mit der Groesse array_size
 *
 */
/*----------------------------------------------------------------------------------*/

void TileIO::createNewTileArray(unsigned short array_size)
{
  if(tile != NULL)
  {
    delete[] tile;
  }
  tile = new Tile[array_size];
  quantity = array_size;
}

/*----------------------------------------------------------------------------------*/
/*!
 * @fn char TileIO::saveTilesToFile(char *filename)
 *
 * @param filename Name der Tile_Datei
 *
 * @brief Speichert das Tile-Array in die angegebe Datei.
 *
 */
/*----------------------------------------------------------------------------------*/

char TileIO::saveTilesToFile(char *filename)
{
  gzFile file;
  TileHeader header;
  char return_value = TLE_OK;
  file = gzopen(filename,"wb");
  if(file != NULL)
  {
    header.quantity = quantity;
    header.version = TLE_FILE_VERSION_1;
    strcpy(header.mark,"TILES");
    if(gzwrite(file,&header,sizeof(TileHeader))==sizeof(TileHeader))
    {
      if(gzwrite(file,(char*)tile,header.quantity*sizeof(Tile))!=(int)sizeof(Tile)*header.quantity)
      {
        return_value = TLE_WRITE_ERROR;
      }
    }
    else
    {
      return_value = TLE_WRITE_ERROR;
    }
  }
  else
  { 
    return_value = TLE_OPEN_ERROR;
  }
  gzclose(file);
  return return_value;    
}

